package com.pi.wms.service;

import com.pi.common.utils.StringUtils;
import com.pi.framework.core.repository.IBaseRepository;
import com.pi.framework.core.service.impl.BaseCrudService;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.wms.entity.TblLedMessage;
import com.pi.wms.repository.LedMessageRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Transactional
@Service
public class LedMessageService extends BaseCrudService<TblLedMessage, Long>{

	@Autowired
	protected LedMessageRepository repository;

    @Override
	protected IBaseRepository<TblLedMessage,Long> getRepository() {
		return this.repository;
	}
	
	@Override
	protected Predicate toPredicate(Root<TblLedMessage> root, CriteriaQuery<?> cq, CriteriaBuilder cb, Map<String, ?> query) {
		List<Predicate> list = new ArrayList<Predicate>();

		String divisionid = (String) query.get("divisionid");
		if (!StringUtils.isNullOrBlank(divisionid)) {
			list.add(cb.equal(root.get("divisionId"), divisionid));
		}

		String code = (String) query.get("code");
        if (!StringUtils.isNullOrBlank(code)) {
			list.add(cb.equal(root.get("code"), "%"+code.trim()+"%"));
		}
		Predicate[] p = new Predicate[list.size()];
		return cb.and(list.toArray(p));
	}

	public TblLedMessage findByCode(String code){
		return this.findByCode(XcmgAmConstants.DIVISION_WH101_ID, code);
	}

	public TblLedMessage findByCode(Long divisionId, String code){
		TblLedMessage leddata=null;
		if (StringUtils.isNullOrBlank(code)){
			return leddata;
		}
		if (divisionId==null){
			divisionId=XcmgAmConstants.DIVISION_WH101_ID;
		}
		leddata=this.repository.findByDivisionIdAndCode(divisionId, code.trim());
		return leddata;
	}

	public TblLedMessage findByFullcode(String fullCode){
		TblLedMessage leddata=null;
		if (StringUtils.isNullOrBlank(fullCode)){
			return leddata;
		}
		leddata=this.repository.findByFullcode(fullCode.trim());
		return leddata;
	}


}
